<?php
/**
 * 厅主管理
 * User: Tommy
 * Date: 2018/11/13
 * Time: 14:46
 */

namespace App\Services;


use App\Models\Agent;
use App\Models\Hall;
use App\Models\HallConfig;
use App\Models\HallDomain;
use App\Models\User;
use common\enum\CommonEnum;
use common\enum\MasterLogEnum;
use Illuminate\Support\Facades\DB;

class HallManageService
{

    /**
     * 厅主列表
     * @param array $values
     * @return array
     */
    public static function hallList(array $values)
    {
        $data = [];
        $domain = HallDomain::select("hall_id", 'domain')->groupBy('hall_id')->get()->toArray();
        $domain = array_column($domain, null, 'hall_id');
        $select = ['a.id', 'a.company_name', 'a.agent_count', 'a.user_count', 'a.created_at', 'a.start_time', 'c.status', 'c.is_use_googlekey', 'c.is_admin_freeze', 'c.is_user_freeze', 'a.admin_name'];
        $query = DB::table('hall as a')
            ->leftJoin('hall_config as c', 'c.hall_id', '=', 'a.id')
            ->select($select);
        if (!empty($values['hall_id'])) {
            $query->where(['a.id' => $values['hall_id']]);
        }
        //修改用户数据
        if (!empty($values['status'])) {
            $query->where(['c.status' => $values['status']]);
        }
        $page = $values['page'] ?? CommonEnum::PAGE_DEFAULT;
        $size = $values['page_size'] ?? CommonEnum::PAGE_SIZE_TWENTY;
        $result = get_page_data($query, $page, $size);
        $data['pagination'] = $result['pagination'];
        foreach ($result['list'] as $val) {
            $val = (array)$val;
            $val['domain']  = !empty($domain[$val['id']]['domain']) ? $domain[$val['id']]['domain'] : '';
            $data['list'][] = $val;
        }
        return $data;
    }

    /**
     * 模糊搜索
     * @param $hallName
     * @return mixed
     */
    public static function search($hallName)
    {
        $select = ['company_name as name', 'id'];
        $query = Hall::select($select);
        $query->whereRaw("LOCATE('" . trim($hallName) . "', company_name)");
        return $query->get()->toArray();
    }

    /**
     * 修改google开关
     * @param $values
     * @return mixed
     */
    public static function updateGoogle(array $values)
    {
        $result   = HallConfig::where(['hall_id' => $values['hall_id']])->update(['is_use_googlekey' => $values['status']]);
        $hallName = Hall::where(['id' => $values['hall_id']])->first()->toArray();
        $remark   = str_replace(['{:type}','{:name}'], [MasterLogEnum::STATUS[$values['status']], $hallName['company_name']], MasterLogEnum::REMARK['google_switch']);
        $status   = $result ? MasterLogEnum::SUCCESS : MasterLogEnum::FAIL;
        MasterLogService::writeMasterLog(MasterLogEnum::GOOGLE_SWITCH, $status, $remark);
        return $result;
    }

    /**
     * 修改运营时间
     * @param array $values
     * @return mixed
     */
    public static function startTime(array $values)
    {
        return Hall::where(['id' => $values['hall_id']])->update(['start_time' => $values['start_time']]);
    }

}